package com.zige.zige.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zige.zige.R;
import com.zige.zige.constants.UrlConsts;
import com.zige.zige.task.PostRequestTask;
import com.zige.zige.task.TaskExecutor;
import com.zige.zige.utils.ToastUtils;
import com.zige.zige.view.MyGridView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 意见反馈
 * Created by Fei on 2015/11/5.
 */
public class UserFeedbackActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout lay_back;
    private TextView tv_titlet;
    private MyGridView gridView;
    private Button btn_ok;
    private EditText ed_useroathe, ed_userSupport;
    private boolean press = true;
    private String textTags = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userfeedback);
        names.add("界面不好");
        names.add("互动体验差");
        names.add("视频不吸引人");
        names.add("视频数量少");
        names.add("经常闪退");
        names.add("功能太少了");
        initView();
        initGridView();
    }

    private void initView() {
        lay_back = serchViewById(R.id.lay_back);
        lay_back.setOnClickListener(this);
        tv_titlet = serchViewById(R.id.title_name);
        tv_titlet.setText("用户反馈");
        ed_useroathe = serchViewById(R.id.ed_useroathe);
        ed_userSupport = serchViewById(R.id.ed_userSupport);
        btn_ok = serchViewById(R.id.btn_comit);
        btn_ok.setOnClickListener(this);
    }

    private void initGridView() {
        gridView = serchViewById(R.id.gridView);
        gridView.requestFocus();
        gridView.setAdapter(new Adapter());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_back:
                finish();
                break;
            case R.id.btn_comit:
                if (TextUtils.isEmpty(textTags.replace(" ", "")) && ed_useroathe.getText().toString().length() < 1) {
                    showErrorDialog("吐槽和反馈必须填写其中一项");
                    return;
                }
                commitInfo();
                break;
        }
    }

    ArrayList<String> names = new ArrayList<>();

    private class Adapter extends BaseAdapter {
        final Map<Integer, String> map = new HashMap<Integer, String>();

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = View.inflate(UserFeedbackActivity.this, R.layout.gridview_userfeedback_item, null);
                viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv_type.setText(names.get(position));
            viewHolder.tv_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    press = !viewHolder.tv_type.isSelected();
                    if (press) {
                        viewHolder.tv_type.setSelected(true);
                        viewHolder.tv_type.setBackgroundResource(R.drawable.video_type_selector);
                        viewHolder.tv_type.setTextColor(getResources().getColor(R.color.white));
                        map.put(position, viewHolder.tv_type.getText().toString());
                    } else {
                        map.remove(position);
                        viewHolder.tv_type.setBackgroundResource(R.drawable.video_type_normal);
                        viewHolder.tv_type.setTextColor(getResources().getColor(R.color.color_6A6A6A));
                        viewHolder.tv_type.setSelected(false);
                    }
                    textTags = (map.values().toString()).replace("[", "").replace("]", "").replace("null", "");
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv_type;
    }

    private void commitInfo() {
        HashMap<String, String> params = new HashMap<>();
        params.put("uid", application.getUserInfo().userID);
        params.put("sysContact", textTags);
        params.put("platform", "android");
        params.put("userContent", ed_useroathe.getText().toString());
        params.put("appContent", ed_userSupport.getText().toString());
        showLoadingDialog();
        TaskExecutor.getInstance().execute(new PostRequestTask(params, UserFeedbackActivity.this, UrlConsts.USER_FEEDBACK) {
            @Override
            protected void handleResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    int code = object.getInt("code");
                    if (code == 0000) {
                        closeLoadingDialog();
                        showErrorDialog("谢谢您的反馈，我们会及时查看");
                        startActivity(new Intent(UserFeedbackActivity.this, HomedActivity.class));
                        finish();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            protected void handleError() {
                super.handleError();
                closeLoadingDialog();
            }
        });
    }


}
